home *** CD-ROM | disk | FTP | other *** search
/ ADA Programming Guide / ADA Programming Guide.iso / ada_gwu / adalib2.c < prev    next >
C/C++ Source or Header  |  1996-01-30  |  14KB  |  391 lines

  1. /* liblist.c: translation of code generator read.stl*/
  2.  
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include "config.h"
  7. #include "segment.h"
  8. #include "slot.h"
  9. #include "ifile.h"
  10. #include "miscp.h"
  11. #include "libfp.h"
  12.  
  13. static void load_library();
  14. static void get_local_ref_maps(IFILE *, int);
  15. static long get_cde_slots(IFILE *, Axq);
  16. static void get_slot(IFILE *, char *);
  17. static char *convert_date(char *);
  18. static char *unit_name_name(char *);
  19. static int is_subunit(char *);
  20. static char *formatted_name(char *);
  21. static char *formatted_stub(char *);
  22.  
  23. IFILE *LIBFILE;
  24.  
  25. #ifdef IBM_PC
  26. /* on the PC, a chdir must be undone before program completes.
  27.  * changed_dir is set when directory changed.
  28.  */
  29. #endif
  30.  
  31.  
  32.  
  33.  
  34.  
  35. void main(int argc, char **argv)
  36. {
  37.         int  c;
  38.         int  lib_opt = FALSE;
  39.         int  errflg = 0;
  40.  
  41.         extern int  optind;
  42.         extern char *optarg;
  43.         char *libname,*fname;
  44.         char *t_name;
  45.  
  46. #ifdef IBM_PC
  47. /*      _outtext(" NYU Ada/Ed Librarian Version 1.11.1\n");
  48.         _outtext(" Copyright (C) 1985-1992 by New York University.");
  49. */
  50. #endif
  51.  
  52. #ifndef IBM_PC
  53.         while((c = getopt(argc,argv,"l:"))!=EOF) {
  54. #else
  55.         while((c = getopt(argc,argv,"L:l:"))!=EOF) {
  56.                 if (isupper(c)) c = tolower(c);
  57. #endif
  58.                 switch(c) {
  59.                 case 'l': /* specify library name */
  60.                         lib_opt = TRUE;
  61.                         libname = strjoin(optarg,"");
  62.                         break;
  63.                 case '?':
  64.                         errflg++;
  65.                 }        
  66.         }
  67.  
  68.         fname = (char *)0;
  69.         if (optind < argc) fname = argv[optind];
  70.         if (!lib_opt && fname == (char *)0) {
  71.                 fname = getenv("ADALIB");
  72.                 if (fname!= (char *)0) {
  73. #ifdef IBM_PC
  74.                         fprintf(stderr, "L");
  75. #else
  76.                         fprintf(stderr, "l");
  77. #endif
  78.                         fprintf(stderr,"ibrary defined by ADALIB: %s\n", fname);
  79.                 }
  80.         }
  81.         if ((!lib_opt && fname == (char *)0) || errflg) {
  82.             fprintf(stderr, "Usage: adalib [-l library]\n");
  83.             exit(1);
  84.         }
  85.         if (!lib_opt) {
  86.            libname = emalloc(strlen(fname) + 1);
  87.            strcpy(libname, fname);
  88.         }
  89.  
  90.         t_name = libset(libname);
  91.         LIBFILE = ifopen(LIBFILENAME, "", "r", 0);
  92.  
  93.         load_library();
  94.         exit(0);
  95. }
  96.  
  97. static void load_library()                                                              /*;load_library*/
  98. {
  99.         /*
  100.          * retrieve information from LIBFILE
  101.          * Called only if lib_option and not newlib.
  102.          */
  103.  
  104.         int             i, j, n, m, unumber, nodes, symbols;
  105.         int             comp_status, unit_count, cur_level;
  106.         char    *comp_date, *status_str, *uname, *aisname, *tmp_str;
  107.         char    *main_string, *fmt_name, *fmt_date;
  108.         int             is_main, empty_unit_slots, parent;
  109.         int             ignore;
  110.  
  111.         FILE *fout;
  112.         
  113.         fout = fopen("avllib.$$$","w");
  114.  
  115.         unit_count = getnum(LIBFILE,"lib-unit-count");
  116.         n = getnum(LIBFILE, "lib-n");
  117.         empty_unit_slots = getnum(LIBFILE, "lib-empty-slots");
  118.         tmp_str = getstr(LIBFILE,"lib-tmp-str");
  119.         for (i = 1; i <= unit_count; i++) {
  120.                 uname = getstr(LIBFILE, "lib-unit-name");
  121.                 unumber = getnum(LIBFILE, "lib-unit-number");
  122.                 aisname = getstr(LIBFILE, "lib-ais-name");
  123.                 comp_date = getstr(LIBFILE, "unit-date");
  124.                 symbols = getnum(LIBFILE, "lib-symbols");
  125.                 nodes = getnum(LIBFILE, "lib-nodes");
  126.                 is_main = getnum(LIBFILE, "lib-is-main");
  127.                 if (is_main) {
  128.                         if (streq(unit_name_type(uname), "ma")) {
  129.                                 main_string = "(Interface)";
  130.                                 }
  131.                         else  {
  132.                                 main_string = "  (Main)   ";
  133.                                 }
  134.                 }
  135.                 else {
  136.                         main_string = "";
  137.                 }
  138.                 comp_status = getnum(LIBFILE, "lib-status");
  139.                 status_str = (comp_status) ? "Active  " : "Obsolete";
  140.                 fmt_date = convert_date(comp_date);
  141.                 fmt_name = formatted_name(uname);
  142.  
  143.                 fprintf(fout,"%8s %11s %-15s %s\n",
  144.                     status_str, main_string, fmt_date, fmt_name);
  145.         }
  146.  
  147.         fprintf(fout,"\n");
  148.         n = getnum(LIBFILE, "lib-n");
  149.         if (n) {
  150. /*              fprintf(fout,"stubs \n\n"); */
  151.                 for (i = 1; i <= n; i++) {
  152.                         uname = getstr(LIBFILE, "lib-unit-name");
  153.                         aisname = getstr(LIBFILE, "lib-ais-name");
  154.                         parent = getnum(LIBFILE, "lib-parent");
  155.                         cur_level = getnum(LIBFILE, "lib-cur-level");
  156.                         m = getnum(LIBFILE, "stub-file-size");
  157.                         for (j = 1; j <= m; j++)
  158.                                 ignore = getnum(LIBFILE, "stub-file");
  159. /*                      fprintf(fout,"Stub               %s\n", formatted_stub(uname)); */
  160.                 }
  161. /*              fprintf(fout,"\n");  */
  162.         }
  163.  
  164.         fclose(fout);
  165.         ifclose(LIBFILE);
  166.  
  167.         return;
  168.  
  169. #ifdef TBSL
  170.         n = getnum(LIBFILE, "precedes-map-size");
  171.         printf("precedes map\n");
  172.         for (i = 1; i <= n; i += 2) {
  173.                 dom = getnum(LIBFILE, "precedes-map-dom");
  174.                 m = getnum(LIBFILE, "precedes-map-nelt");
  175.                 printf("  %4d:", dom);
  176.                 for (j = 1; j <= m; j++) {
  177.                         range = getnum(LIBFILE, "precedes-map-ent");
  178.                         printf(" %4d", range);
  179.                 }
  180.                 printf("\n");
  181.         }
  182.         n = getnum(LIBFILE, "compilation_table_size");
  183.         if (n) {
  184.                 printf("\ncompilation table\n");
  185.                 for (i = 1; i <= n; i++) {
  186.                         unum = (int) getnum(LIBFILE, "compilation-table-ent");
  187.                         printf("  %d\n", unum);
  188.                 }
  189.                 printf("\n");
  190.         }
  191.         /* late_instances */
  192.         n = getnum(LIBFILE, "late-instances-size");
  193.         if (n) {
  194.                 printf("late instances\n");
  195.                 for (i = 1; i <= n; i++) {
  196.                         str = (char *) getstr(LIBFILE, "late-instances-str");
  197.                         printf("  %s\n", str);
  198.                 }
  199.         }
  200.         /* current code segment */
  201.         n = getnum(LIBFILE, "unit-size");
  202.         printf("\ncurrent code segments\n");
  203.         printf("  unit cs\n");
  204.         for (i = 1; i <= n; i++) {
  205.                 cs = getnum(LIBFILE, "current-code-segment");
  206.                 if (cs) printf("   %d: %d\n", i, cs);
  207.         }
  208.         /* local reference maps */
  209.         n = getnum(LIBFILE, "unit-size");
  210.         get_local_ref_maps(LIBFILE, n);
  211.         cde_pos = get_cde_slots(LIBFILE, axq);
  212.  
  213.  
  214.         /* could free axq_data_slots, etc., but keep for now */
  215.         /* read out LIB_STUB map (always empty for now) */
  216.         ifclose(LIBFILE);
  217.         return;
  218. #endif
  219. }
  220.  
  221. static void get_local_ref_maps(IFILE *ifile, int units) /*;get_local_ref_map*/
  222. {
  223.         int             unit, defined, i, off, n;
  224.         int             sym_seq, sym_unit;
  225.  
  226.         printf("\nlocal reference maps\n");
  227.         for (unit = 1; unit <= units; unit++) {
  228.                 /* ignore empty ref maps (predef units) and obselete units */
  229.                 defined = getnum(ifile, "local-ref-map-defined");
  230.                 if (!defined) continue;
  231.                 printf("%d: ", unit);
  232.                 n = getnum(ifile, "local-ref-map-size");
  233.                 n = n/2;
  234.                 for (i = 1; i <= n; i++) {
  235.                         sym_seq = getnum(ifile, "local-ref-map-sym-seq");
  236.                         sym_unit = getnum(ifile, "local-ref-map-sym-unit");
  237.                         off = getnum(ifile, "local-ref-map-off");
  238.                         /* if all three values are zero ignore this entry. It is a fake
  239.                          * entry created by put_local_ref_map. see comment there.
  240.                          */
  241.                         if (sym_seq == 0 && sym_unit == 0 && off == 0) continue;
  242.                         printf("%d %d %d ", sym_seq, sym_unit, off);
  243.                 }
  244.                 printf("\n");
  245.         }
  246.         printf("\n");
  247. }
  248.  
  249. static long get_cde_slots(IFILE *file, Axq axq)                         /*;get_cde_slots*/
  250. {
  251.         long    dpos;
  252.         int             n_code, n_data, n_exception;
  253.  
  254.  
  255.         dpos = file->fh_slots;
  256.         /* position to start of slot info */
  257.         ifseek(file, "get-cde-slots-start", dpos, 0);
  258.         n_code = getnum(file, "n-code");
  259.         n_data = getnum(file, "n-data");
  260.         n_exception = getnum(file, "n-exception");
  261.         get_slot(file, "code");
  262.         get_slot(file, "data");
  263.         get_slot(file, "exceptions");
  264.         return dpos; /* return offset of start of slot info */
  265. }
  266.  
  267. static void get_slot(IFILE *file, char *name)                                   /*;get_slot*/
  268. {
  269.         /* This procedure reads in the SLOTS information. 
  270.          * Entries are Slots structures. nmax is guess at needed dimension,
  271.          * dim is set to dimension actually found.
  272.          */
  273.  
  274.         int i, n, exists;
  275.         int     slot_seq, slot_unit, slot_number;
  276.         char *slot_name;
  277.  
  278.         n = getnum(file, "slot-entries");
  279.         printf("%s slots \n", name);
  280.         printf("  num seq unit  name\n");
  281.  
  282.         for (i = 1; i <= n; i++) {
  283.                 exists = getnum(LIBFILE, "slot-exists");
  284.                 if (exists) {
  285.                         slot_seq = getnum(LIBFILE, "slot-seq");
  286.                         slot_unit = getnum(LIBFILE, "slot-unit");
  287.                         slot_number = getnum(LIBFILE, "slot-number");
  288.                         slot_name = getstr(LIBFILE, "slot_name");
  289.                         printf("  %3d %3d %4d  %s\n",
  290.                             slot_number, slot_seq, slot_unit, slot_name);
  291.                 }
  292.         }
  293.         printf("\n");
  294. }
  295.  
  296. static char *convert_date(char *comp_date)                                      /*;convert_date*/
  297. {
  298.         static char new_date[15];
  299.         int     i;
  300.  
  301.         if (comp_date == (char *)0)
  302.                 return(" ");
  303.         comp_date++;
  304.         comp_date++;
  305.         for (i = 6; i < 8; i++)
  306.                 new_date[i] = *comp_date++;
  307.         comp_date++;
  308.         for (i = 0; i < 2; i++)
  309.                 new_date[i] = *comp_date++;
  310.         new_date[2] = '/';
  311.         comp_date++;
  312.         for (i = 3; i < 5; i++)
  313.                 new_date[i] = *comp_date++;
  314.         new_date[5] = '/';
  315.         new_date[8] = ' ';
  316.         comp_date++;
  317.         for (i = 9; i < 14; i++)
  318.                 new_date[i] = *comp_date++;
  319.         new_date[11] = ':';
  320.  
  321.         new_date[14] = '\0';
  322.         return new_date;
  323. }
  324.  
  325. static char *unit_name_name(char *u)                                            /*;unit_name_name*/
  326. {
  327.         int     n;
  328.         char    *s1, *s2;
  329.  
  330.         n = strlen(u);
  331.         if (n <= 2)
  332.                 return (char *)0;
  333.  
  334.         s1 = u+2;                               /* point to start of name*/
  335.         s2 = strchr(s1, '.');   /* look for dot after first name */
  336.         if (s2 == (char *)0)    /* if no dot take rest of string */
  337.                 s2 = u + n;             /* find end */
  338.         n = s2 - s1;
  339.         s2 = emalloc((unsigned) n+1);
  340.         strncpy(s2, s1, n);
  341.         s2[n] = '\0'; /* terminate result */
  342.         return (s2);
  343. }
  344.  
  345. static int is_subunit(char *u)                                                                  /*;is_subunit*/
  346. {
  347.         /* In C, IS_SUBUNIT is procedure is_subunit():
  348.          *      IS_SUBUNIT(na);           (#na > 2)                          endm;
  349.          */
  350.  
  351.         int     n;
  352.         char    *s1, *s2;
  353.  
  354.         if (u == (char *)0)
  355.                 chaos("is_subunit: null pointer");
  356.         n = strlen(u);
  357.         if (n <= 2)
  358.                 return FALSE;
  359.         s1 = u + 2; /* point to start of name*/
  360.         s2 = strchr(s1, '.'); /* look for dot after first name */
  361.         if (s2 == (char *)0) /* if no dot take rest of string */
  362.                 return FALSE;
  363.         return TRUE; /* if subunit*/
  364. }
  365.  
  366. static char *formatted_name(char *unit)                                 /*;formatted_name*/
  367. {
  368.         char *kind, *unit_kind;
  369.  
  370.         kind = unit_name_type(unit);
  371.         if (is_subunit(unit))       unit_kind = "proper body ";
  372.         else if (streq(kind, "sp"))  unit_kind = "package spec ";
  373.         else if (streq(kind, "bo"))  unit_kind = "package body ";
  374.         else if (streq(kind, "ss"))  unit_kind = "subprogram spec ";
  375.         else if (streq(kind, "su"))  unit_kind = "subprogram ";
  376.         else if (streq(kind, "ma"))  unit_kind = "binding unit ";
  377.         else unit_kind = "unit ";
  378.         return strjoin(unit_kind, unit_name_name(unit));
  379. }
  380.  
  381. static char *formatted_stub(char *unit)                                         /*;formatted_stub*/
  382. {
  383.         char *kind, *unit_kind;
  384.  
  385.         kind = unit_name_type(unit);
  386.         if (streq(kind, "bo"))  unit_kind = "package (task) stub ";
  387.         else if (streq(kind, "su"))  unit_kind = "subprogram stub ";
  388.         else unit_kind = "stub ";
  389.         return strjoin(unit_kind, unit_name_name(unit));
  390. }
  391.